/**
 * @author tibi
 */
Ext.namespace("Softab");
Softab.EntityLookupWindow = function(sender, entityID, singleSelect, valueField, textField){
	
    Ext.Ajax.request({
        url: 'server/EntityGrid.php?definition=true&__id=' + entityID,
        success: function(response){
            r = response.responseText;
            var gridDefinition = eval(r);
            var StoreDefinition = eval(gridDefinition.Store);
            var ColumnsModel = eval(gridDefinition.ColumnModel);
            
            EntityLookupDataStore = new Ext.data.Store({
                id: entityID + '_EntityLookupDataStore',
                proxy: new Ext.data.HttpProxy({
                    url: 'server/EntityGrid.php?definition=false&__id=' + entityID, // File to connect to
                    method: 'POST'
                }),
                baseParams: {
                    task: "LISTING"
                },
                reader: new Ext.data.JsonReader({ // we tell the datastore where to get his data from
                    root: 'results',
                    totalProperty: 'total',
                    id: 'id'
                }, StoreDefinition),
                sortInfo: {
                    field: 'ID',
                    direction: "ASC"
                }
            });
            EntityLookupColumnModel = new Ext.grid.ColumnModel(ColumnsModel);
            EntityLookupColumnModel.defaultSortable = true;
            EntityLookupGrid = new Ext.grid.GridPanel({
                id: entityID + "_LookupGrid",
                //title: node.text,
                store: EntityLookupDataStore, // the datastore is defined here
                cm: EntityLookupColumnModel, // the columnmodel is defined here
                enableColLock: false,
                clicksToEdit: 1,
                width: 700,
                height: 560,
                tbar: [new Ext.Button({
						text: "Save Selection",
						handler: function(){
							sender.setSelected(Softab.EntityLookupWindow.SelectedValues, singleSelect);
							Softab.EntityLookupWindow.SelectedValues = 'undefined';
							win.close();
						}
					}),
					new Ext.Button({
						text: "Cancel",
						handler: function(){
							Softab.EntityLookupWindow.SelectedValues = 'undefined';
							win.close();
						}
					}),
					'-', new Ext.app.SearchField({
                    store: EntityLookupDataStore,
                    params: {
                        start: 0,
                        limit: 15
                    },
                    width: 120
                })],
                bbar: new Ext.PagingToolbar({
                    pageSize: 15,
                    store: EntityLookupDataStore,
                    displayInfo: true
                }),
                selModel: new Ext.grid.RowSelectionModel({
                    singleSelect: singleSelect
                })
            });
            EntityLookupGrid.on('rowclick', function(grid, rowIndex, ev){
				Softab.EntityLookupWindow.ChangeSelectedValues(grid, rowIndex, ev);
			});
            EntityLookupDataStore.load({
                params: {
                    start: 0,
                    limit: 15
                }
            });
            EntityLookupGrid.closable = true;
            var win = new Ext.Window({
                width: 720,
                id: 'entity-lookup-win',
                autoScroll: true,
                modal: true,
                height: 600,
                onEsc: function(){
                    this.close()
                },
                title: 'Entity lookup ',
                listeners: {
                    show: function(){
                        this.loadMask = new Ext.LoadMask(this.body, {
                            msg: 'Loading. Please wait...'
                        });
                    }
                }
            });
            win.add(EntityLookupGrid);
            win.show();
            Ext.WindowMgr.bringToFront(win);
        }
    });
    
    //sender.setSelectedText("Text Changed From Window");
    //sender.setSelectedValue("Value Changed From Window");
}
Softab.EntityLookupWindow.prototype.SelectedValues = 'undefined';
Softab.EntityLookupWindow.ChangeSelectedValues = function(grid, rowIndex, ev){
	selectedRecord = grid.getStore().getAt(rowIndex);
	Softab.EntityLookupWindow.SelectedValues = grid.getSelectionModel().getSelections();
}
